/* 
 * Enterprise Library Extensions - DI + ORM.
 * Copyright (C) 2006
 *
 * GNU General Public License
 *
 * This program is free software; you can redistribute 
 * it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either version 2 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 */
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;
using System.Diagnostics;
using ELE.EnterpriseLibrary.Data.Configuration.Design.Properties;

namespace ELE.EnterpriseLibrary.Data.Configuration.Design
{
	/// <summary>
	/// Represents a command for adding the O/R Mapping Application Block to the current application.
	/// </summary>
	public class AddObjectMappingSettingsNodeCommand : AddChildNodeCommand
	{
		/// <summary>
		/// Initialize a new instance of the <see cref="AddObjectMappingSettingsNodeCommand"/> class with an <see cref="IServiceProvider"/>.
		/// </summary>
		/// <param name="serviceProvider">The a mechanism for retrieving a service object; that is, an object that provides custom support to other objects.</param>
		public AddObjectMappingSettingsNodeCommand(IServiceProvider serviceProvider)
			: base(serviceProvider, typeof(ObjectMappingSettingsNode))
		{

		}

		/// <summary>
		/// <para>Adds the <see cref="ObjectMappingSettingsNode"/> to the current application with a default <see cref="SqlQueryNode"/> and <see cref="ObjectSqlMappingNode"/>.</para>
		/// </summary>
		/// <param name="e"><para>An <see cref="EventArgs"/> containing the event data.</para></param>
		protected override void OnExecuted(EventArgs e)
		{
			base.OnExecuted(e);
			ObjectMappingSettingsNode node = ChildNode as ObjectMappingSettingsNode;
			Debug.Assert(null != node, "The added node should be a ObjectMappingSettingsNode");

      QueryBuilderCollectionNode queryBuilderCollectionNode = new QueryBuilderCollectionNode();
      node.AddNode(queryBuilderCollectionNode);
      QueryBuilderData qbData = new QueryBuilderData(Resource.DefaultQueryBuilderName);
      qbData.Default = true;
      qbData.Type = (typeof(ELE.EnterpriseLibrary.Data.GenericSqlQueryBuilder)).AssemblyQualifiedName;
      queryBuilderCollectionNode.AddNode(new QueryBuilderNode(qbData));

      ObjectTypeAssemblyCollectionNode assembliesNode = new ObjectTypeAssemblyCollectionNode();
      node.AddNode(assembliesNode);
      ObjectTypeDefCollectionNode typesNode = new ObjectTypeDefCollectionNode();
      node.AddNode(typesNode);

      SqlQueryCollectionNode sqlQueryCollectionNode = new SqlQueryCollectionNode();
      node.AddNode(sqlQueryCollectionNode);
      int defaultNodeIdx = sqlQueryCollectionNode.AddNode(new SqlQueryNode());
      node.DefaultSqlQuery = (SqlQueryNode)sqlQueryCollectionNode.Nodes[defaultNodeIdx];

      ObjectSqlMappingCollectionNode sqlMappingCollectionNode = new ObjectSqlMappingCollectionNode();
      node.AddNode(sqlMappingCollectionNode);
      int defaultMNodeIdx = sqlMappingCollectionNode.AddNode(new ObjectSqlMappingNode());
      node.DefaultObjectSqlMapping = (ObjectSqlMappingNode)sqlMappingCollectionNode.Nodes[defaultMNodeIdx];

		}
	}
}
